package com.ktjy.controller;

import com.ktjy.entity.SysUser;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import java.util.Map;

@Controller
@RequestMapping("/dev")
public class HelloController extends AbstractController {
    private static final Logger logger = LoggerFactory.getLogger(HelloController.class);

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
        logger.info("Spring MVC框架搭建成功，当前请求路径：{}",request.getRequestURL());
        return new ModelAndView("hello");
    }

    @RequestMapping("/hello")
    protected ModelAndView hello(@RequestParam(name="realName",required = false) String name) throws Exception {
        logger.info("你好{}，欢迎学习SpringMVC！",name);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("realName",name);
        modelAndView.setViewName("hello");
        return modelAndView;
    }
    @RequestMapping("/model")
    public String model(Map map, @RequestParam(name="realName",required = false)String name) throws Exception {
        logger.info("你好{}，在参数注入Model对象也可以出参！",name);
        map.put("realName",name);
        SysUser sysUser = new SysUser();
        sysUser.setRealName(name);
        map.put("sysUser",sysUser);
        return "hello";
    }
}
